import requests
import re
import json

class DangDangBookTop500:
    '''
        当当网图书畅销榜 TOP500，使用正则表达式解析html，数据保存到文本文件
    '''

    def __init__(self) -> None:
        self.html_pattern = re.compile(
            '<li.*?list_num.*?(\d+)\.</div>.*?<img src="(.*?)".*?class="name".*?title="(.*?)">.*?class="star">.*?class="tuijian">(.*?)</span>.*?class="publisher_info">.*?target="_blank">(.*?)</a>.*?class="biaosheng">.*?<span>(.*?)</span></div>.*?<p><span class="price_n">(.*?)</span>.*?</li>', re.S)
        self.url = 'http://bang.dangdang.com/books/fivestars/01.00.00.00.00.00-recent30-0-0-1-{page}'

    def request_page(self, page):
        try:
            response = requests.get(self.url.format(page=page))
            if response.status_code == 200:
                return response.text
        except requests.RequestException as e:
            print(e)
            return None

    def parse_result(self, html):
        items = re.findall(self.html_pattern, html)

        for item in items:
            yield {
                'range': item[0],
                'image': item[1],
                'title': item[2],
                'recommend': item[3],
                'author': item[4],
                'times': item[5],
                'price': item[6]
            }


    def write_item_to_file(self, item):
        print('开始写入数据 ====> ' + str(item))
        with open('book.txt', 'a', encoding='UTF-8') as f:
            f.write(json.dumps(item, ensure_ascii=False) + '\n')

    def run(self):
        for i in range(1, 26):      
            html = self.request_page(i)
            items = self.parse_result(html)  # 解析过滤我们想要的信息
            for item in items:
                self.write_item_to_file(item)

if __name__ == "__main__":
    dd = DangDangBookTop500()
    dd.run()
